package ext.ux.files.server;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * A context listener which initialize Spring application context
 */
public class ContextListener implements ServletContextListener {
  
  private static ApplicationContext factory;
  
  /* (non-Javadoc)
   * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
   */
  public void contextInitialized(ServletContextEvent evtP) {
      initSpring(evtP);
  }

  /**
   * Initialize Spring application context
   */
  private void initSpring(ServletContextEvent evt) {
    factory = WebApplicationContextUtils.getWebApplicationContext(evt.getServletContext());
  }

  /* (non-Javadoc)
   * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
   */
  public void contextDestroyed(ServletContextEvent arg0P) {
  }

  /**
   * Return the Spring factory
   * @return
   */
  public static ApplicationContext getFactory() {
    return factory;
  }

}
